package com.mouse.hxsj.view;

import com.mouse.damo.micro.Com;
import com.mouse.damo.micro.Sleep;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.service.BaoService;
import com.mouse.hxsj.util.StringUtil;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@org.springframework.stereotype.Component
public class BaoFrm extends JInternalFrame {

    JPanel main_panel;
    JPanel old_panel;
    BaoService baoService = null;
    JButton button_3;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BaoFrm frame = new BaoFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public BaoFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("等图");
        setBounds(100, 100, 550, 400);

        JLabel label = new JLabel("主号：");

        main_panel = new JPanel();

        JButton btnNewButton_1 = new JButton("激活");

        JLabel label_1 = new JLabel("副号：");

        old_panel = new JPanel();

        JButton button_2 = new JButton("激活");

        button_3 = new JButton("开启");
        button_3.addActionListener(e -> open());
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addComponent(label)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(main_panel, GroupLayout.PREFERRED_SIZE, 358, GroupLayout.PREFERRED_SIZE))
        				.addGroup(groupLayout.createSequentialGroup()
        					.addComponent(label_1)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(old_panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addPreferredGap(ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
        					.addComponent(btnNewButton_1)
        					.addContainerGap())
        				.addGroup(groupLayout.createSequentialGroup()
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addComponent(button_2)
        					.addContainerGap())))
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGap(127)
        			.addComponent(button_3)
        			.addContainerGap(350, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addGap(20)
        					.addComponent(label))
        				.addGroup(groupLayout.createSequentialGroup()
        					.addContainerGap()
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addComponent(main_panel, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
        						.addComponent(btnNewButton_1))))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(label_1)
        				.addComponent(old_panel, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
        				.addComponent(button_2))
        			.addGap(71)
        			.addComponent(button_3)
        			.addContainerGap(174, Short.MAX_VALUE))
        );
        old_panel.setLayout(new GridLayout(1, 5, 0, 0));
        main_panel.setLayout(new GridLayout(1, 5, 0, 0));
        getContentPane().setLayout(groupLayout);
        baoService = new BaoService();
        baoService.setButton(button_3);
    }

    private void initHwnd() {
        List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        main_panel.removeAll();
        ButtonGroup buttonGroup = new ButtonGroup();
        for (int i = 0; i < comList.size(); i++) {
            Com com = comList.get(i);
            String account = accountList.get(i);
            JCheckBox jCheckBox = new JCheckBox(com.getHwnd() + "-" + (StringUtil.isBlank(account) || account.equals("0") ? "0" : account.substring(0, 3)));
            jCheckBox.addActionListener(e -> {
                if (jCheckBox.isSelected()) {
                    String text = jCheckBox.getText().split("-")[0];
                    old_panel.removeAll();
                    for (int j = 0; j < comList.size(); j++) {
                        Com com2 = comList.get(j);
                        String account2 = accountList.get(j);
                        if (Integer.valueOf(text).equals(com2.getHwnd())) continue;
                        JCheckBox jCheckBox2 = new JCheckBox(com2.getHwnd() + "-" + (StringUtil.isBlank(account2)
                                || account2.equals("0") ? "0" : account2.substring(0, 3)));
                        old_panel.add(jCheckBox2);
                    }
                    old_panel.revalidate();
                }
            });
            main_panel.add(jCheckBox);
            buttonGroup.add(jCheckBox);
        }
    }

    boolean isFirst = true;

    private void open() {
        if (!isFirst) {
            if (button_3.getText().equals("关闭")) {
                button_3.setText("开启");
                baoService.setMianOpen(false);
            } else {
                button_3.setText("关闭");
                baoService.setMianOpen(true);
            }
            return;
        }
        Component[] components = main_panel.getComponents();
        Component[] old_components = old_panel.getComponents();
        Integer mainHwnd = null;
        for (Component component : components) {
            JCheckBox jCheckBox = (JCheckBox) component;
            if (jCheckBox.isSelected()) {
                mainHwnd = Integer.valueOf(jCheckBox.getText().split("-")[0]);
                break;
            }
        }
        List<Integer> otherHwnd = new ArrayList<>();
        for (Component component : old_components) {
            JCheckBox jCheckBox = (JCheckBox) component;
            if (jCheckBox.isSelected()) {
                otherHwnd.add(Integer.valueOf(jCheckBox.getText().split("-")[0]));
            }
        }

        if (mainHwnd == null) return;
        baoService.setMainHwnd(mainHwnd);
        new Thread(() -> {
            baoService.mainWait();
        }).start();
        if (otherHwnd.size() != 0) {
            baoService.setOtherHwndList(otherHwnd);
            new Thread(() -> {
                baoService.otherFly();
            }).start();
        }
        Sleep.sleep(100);
        baoService.setMianOpen(true);
        button_3.setText("关闭");
        isFirst = false;
    }

    public void initFrm() {
        button_3.setText("开启");
        old_panel.removeAll();
        initHwnd();
    }
}
